package com.spit.tph.nike_mvp.adapter

import CONSUMABLES_RECEIVE
import CONSUMABLES_RESTOCK
import CONSUMABLES_WAREHOUSE
import android.app.Activity
import android.content.Context
import android.support.v7.widget.AppCompatButton
import android.support.v7.widget.AppCompatEditText
import android.support.v7.widget.AppCompatTextView
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Filter
import android.widget.Filterable
import com.csl.ams.nike_mvp.recyclerview.ViewHolder
import com.csl.ams.nike_mvp.utils.PopupWindowTool
import com.spit.tph.R
import com.spit.tph.nike_mvp.utils.ToastUtil
import com.yc.reid.adapter.BaseRecyclerviewAdapter
import com.yyc.stocktake.bean.DataBean

/**
 * User: Nike
 *  2024/2/23 17:53
 */
class ConsumablesDescAdapter(act: Context, listBean: List<DataBean>, mCStatus: Int) :
    BaseRecyclerviewAdapter<DataBean>(act, listBean), Filterable {

    val mStatus = mCStatus

    override fun onCreateViewHolde(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            LayoutInflater.from(parent.context).inflate(R.layout.i_consumables_desc, parent, false)
        )
    }

    override fun onBindViewHolde(viewHolder: ViewHolder, position: Int) {
        val bean = listBean[position]
        bean.run {
            viewHolder.setText(R.id.tv_title, consumablesNo + "/" + consumablesName)
            val unlimited = act.getString(R.string.unlimited)
            viewHolder.setText(R.id.search_cell_brand_value, specs)
            viewHolder.setText(R.id.search_cell_model_value, categoryName)
            viewHolder.setText(R.id.search_cell_location_value, currentInventory)
            viewHolder.setText(R.id.search_cell_category_value, unit)
            viewHolder.setText(R.id.tv_limited_use, "${if (singleClaimLimit == 0) unlimited else singleClaimLimit}")
            viewHolder.setText(R.id.tv_storage_limit, "${if (inventoryUpper == 0) unlimited else inventoryUpper}")
            val etNumber = viewHolder.getView<AppCompatEditText>(R.id.et_number)
            val etRecruiter = viewHolder.getView<AppCompatEditText>(R.id.et_recruiter)
            val etRemark = viewHolder.getView<AppCompatEditText>(R.id.et_remark)
            status = mStatus
            when(mStatus){
                CONSUMABLES_RECEIVE ->{
                    viewHolder.setText(R.id.tv_quantity, act.getText(R.string.quantity_received))
                    viewHolder.setViewVisibility(R.id.ly_limited_use, View.VISIBLE)
                }
                CONSUMABLES_WAREHOUSE ->{
                    viewHolder.setText(R.id.tv_quantity, act.getText(R.string.inbound_quantity))
                    viewHolder.setViewVisibility(R.id.ly_storage_limit, View.VISIBLE)
                }
                CONSUMABLES_RESTOCK ->{
                    viewHolder.setText(R.id.tv_quantity, act.getText(R.string.replenishment_quantity))
                }

                else -> {

                }
            }

            etNumber.setOnClickListener {
                val numText = etNumber.text.toString()
                if (numText.isNotEmpty()) {
                    bean.num = numText.toInt()
                }
                mItemClickListener?.onItemClick(position, bean, it)
            }

            viewHolder.getView<AppCompatButton>(R.id.bt_jia).setOnClickListener {
                try {
                    val toString = etNumber.text.toString()
                    var toInt = toString.toInt()
                    toInt +=1
                    etNumber.setText("$toInt")
                } catch (e: NumberFormatException) {

                }
            }

            viewHolder.getView<AppCompatButton>(R.id.bt_jian).setOnClickListener {
                try {
                    if (num <= 1){
                        return@setOnClickListener
                    }
                    num -= 1
                    etNumber.setText("$num")
                } catch (e: NumberFormatException) {

                }
            }

            val textWatcherNumber: TextWatcher = object : TextWatcher {
                override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
                override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
                override fun afterTextChanged(s: Editable) {
                    if (s.isNotEmpty()) {
                        val toInt = s.toString().toInt()
                        when(mStatus){
                            CONSUMABLES_RECEIVE ->{
                                if ((toInt > currentInventory) || (singleClaimLimit != 0 && toInt > singleClaimLimit)) {
                                    ToastUtil.showShortToast(act.getString(R.string.text2))
                                    if (num != toInt) {
                                        etNumber.setText("$num")
                                    }
                                    return
                                }
                            }
                            CONSUMABLES_WAREHOUSE ->{
                                if (singleClaimLimit != 0 && toInt > inventoryUpper){
                                    ToastUtil.showShortToast(act.getString(R.string.text11))
                                    if (num != toInt) {
                                        etNumber.setText("$num")
                                    }
                                    return
                                }
                            }
                        }
                        num = toInt
                        if (num != toInt) {
                            etNumber.setText("$num")
                        }
                    }
                }
            }
            //入库数量
            if (etNumber.getTag() is TextWatcher) {
                etNumber.removeTextChangedListener(etNumber.getTag() as TextWatcher)
            }
            etNumber.setText("$num")
            etNumber.addTextChangedListener(textWatcherNumber)
            etNumber.setTag(textWatcherNumber)

            //领用人
            val textWatcherRecruiter: TextWatcher = object : TextWatcher {
                override fun beforeTextChanged(s: CharSequence, start: Int, count: Int,  after: Int) {}
                override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
                override fun afterTextChanged(s: Editable) {
                    ChangeUser = s.toString()
                }
            }
            setTextWatcher(etRecruiter, textWatcherRecruiter, ChangeUser)

            //备注
            val textWatcherRemarks: TextWatcher = object : TextWatcher {
                override fun beforeTextChanged(s: CharSequence, start: Int, count: Int,  after: Int) {}
                override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
                override fun afterTextChanged(s: Editable) {
                    remark = s.toString()
                }
            }
            setTextWatcher(etRemark, textWatcherRemarks, remark)
        }


        viewHolder.setOnItemClickListener(object : View.OnClickListener {
            override fun onClick(p0: View?) {
            }
        })
        viewHolder.setOnItemLongClickListener(object :View.OnLongClickListener{
            override fun onLongClick(p0: View?): Boolean {
                mItemLongClickListener?.onItemClick(position, bean, p0!!)
                return true
            }
        })
    }

    private fun setTextWatcher(
        etView: AppCompatEditText,
        viewRecruiter: TextWatcher,
        text: String
    ) {
        if (etView.getTag() is TextWatcher) {
            etView.removeTextChangedListener(etView.getTag() as TextWatcher)
        }
        etView.setText(text)
        etView.addTextChangedListener(viewRecruiter)
        etView.setTag(viewRecruiter)
    }

    var mFilterList = ArrayList<DataBean>()

    fun appendList(list: List<DataBean>) {
        listBean = list as MutableList<DataBean>
        //这里需要初始化filterList
        mFilterList = list as ArrayList<DataBean>
    }

    override fun getFilter(): Filter {
        return object : Filter() {
            //执行过滤操作
            override fun performFiltering(charSequence: CharSequence): FilterResults {
                val charString = charSequence.toString()
                if (charString.isEmpty()) {
                    //没有过滤的内容，则使用源数据
                    mFilterList = listBean as ArrayList<DataBean>
                } else {
                    val filteredList: MutableList<DataBean> = ArrayList()
                    for (i in listBean.indices) {
                        val bean = listBean[i]
                        val epc = bean.epc
                        val assetNo = bean.assetNo
                        val WheelchairNo = bean.WheelchairNo
                        if (
//                            epc?.contains(charString, ignoreCase = true) == true
                            assetNo?.contains(charString, ignoreCase = true) == true
//                            || WheelchairNo?.contains(charString, ignoreCase = true) == true
                        ) {
                            filteredList.add(bean)
                        }
                    }
                    mFilterList = filteredList as ArrayList<DataBean>
                }
                val filterResults = FilterResults()
                filterResults.values = mFilterList
                return filterResults
            }

            //把过滤后的值返回出来
            override fun publishResults(charSequence: CharSequence, filterResults: FilterResults) {
                mFilterList = filterResults.values as ArrayList<DataBean>
                notifyDataSetChanged()
                /*val filteredList = filterResults.values as? ArrayList<DataBean>
                if (filteredList != null) {
                    mFilterList = filteredList
                    notifyDataSetChanged()
                }*/
            }
        }
    }

    override fun getItemCount(): Int {
        return listBean.size
    }

    private var mItemClickListener: OnItemClickListener? = null

    fun setOnItemClickListener(l: OnItemClickListener?) {
        mItemClickListener = l
    }

    interface OnItemClickListener {
        fun onItemClick(position: Int, item: DataBean, view: View)
    }

    private var mItemLongClickListener: OnItemLongClickListener? = null

    fun setOnItemLongClickListener(l: OnItemLongClickListener?) {
        mItemLongClickListener = l
    }

    interface OnItemLongClickListener {
        fun onItemClick(position: Int, item: DataBean, view: View)
    }

    private var mItemTextClickListener: OnItemTextClickListener? = null

    fun setOnItemTextClickListener(l: OnItemTextClickListener?) {
        mItemTextClickListener = l
    }

    interface OnItemTextClickListener {
        fun onTeam(position: Int, item: DataBean)
        fun onMaterialPoints(position: Int, item: DataBean)
    }

}